/////-----开票管理页面   结算/批量结算操作  ----------


import { cloneObject } from '@/utils/util'
import InvoiceBalance from '@views/invoice/balance/invoiceBalance.vue'
import InvoiceBatchSettlement from '@views/invoice/balance/invoiceBatchSettlement.vue'
import SettlementRemainPaid from '@views/invoice/balance/settlementRemainPaid.vue'
import InvoiceBatchOverLimit from '@views/invoice/balance/invoiceBatchOverLimit.vue'
import ChangeCollectionMethod from '@views/invoice/common/changeCollectionMethod.vue'  //更改征收方式弹层
import InvoiceCoverPrompt from '@views/invoice/common/invoiceCoverPrompt.vue'  //超额免征超服务模式弹层提示
import PaymentRevocation from '@views/invoice/balance/paymentRevocation.vue'

const balance = {
  components:{
    InvoiceBalance,
    InvoiceBatchSettlement,
    SettlementRemainPaid,
    InvoiceBatchOverLimit,
    ChangeCollectionMethod,
    InvoiceCoverPrompt,
    PaymentRevocation,
  },
  data(){
    return {
      //--------结算与批量结算-------
      //弹层展示各类金额
      promptColumn:{
        totalYear: 0,  //年度累计开票金额（含本次）
        totalQuarter: 0,  //季度累计开票金额（含本次）
        totalMonth: 0,  //月度累计开票金额（含本次）
        digitalMonthLimit: 0,   //数电月限额
        monthLimit: 0,  //服务模式月限额
        quarterLimit: 0,  //季度最大开票限额
        digitalRemaining: 0,   //数电剩余额度
        yearTotalMoney: 0,  //年度剩余开票金额
        quarterTotalMoney: 0,  //季度剩余开票金额
        monthTotalMoney: 0,  //月度剩余开票金额
      },
      //未跨阶梯（预估费用）
      balanceConfig: {
        width: '450px',
        visible: false,
        ladderStatus: 1, //1-未跨阶梯  2-跨阶梯
        pendingOrders: 1, //1-有待支付订单  2-无待支付订单
        source: 4,    //来源  3-单笔结算  4-批量结算
      },
      //跨阶梯（展示本季和历史费用）
      batchConfig:{
        width: '850px',
        visible: false,
        ladderStatus: 1, //1-未跨阶梯  2-跨阶梯
        pendingOrders: 1, //1-有待支付订单  2-无待支付订单
        source: 3,    //来源  3-单笔结算  4-批量结算
      },
      //单笔结算-展示超限额等超额判断弹层
      promptConfig: {
        width: '650px',
        visible: false,    //modal展示状态
        modalTitle: '',   //modal层的标题
        title: '',     //标题
        content: '',  //内容
        okText: '继续提交',    //确定文字
        okType: 'primary',    //确定类型
        cancleText: '暂不提交',    //取消文字
        cancleType: 'default',    //取消类型
        expandText: '',    //拓展文字
        source: 3,    //来源  1-在途  2-无在途  3-单笔结算  4-批量结算
        category: '',    //类别
      },
      //批量结算-展示超限额等超额判断弹层
      overLimitConfig: {
        width: '750px',
        visible: false,
        modalTitle: '',
        pendingOrders: 1, //1-有待支付订单  2-无待支付订单
        category: '',    //类别  cnd-超470万  sd-超数电月限额  czs-超征收限额  cfwms-超服务模式限额
        settlableLimit: 0,   //可结算额度
      },
      //征收方式变更弹层
      collectionConfig: {
        width: '800px',
        visible: false,
        source: '',   //来源   3-单笔结算  4-批量结算
        base: 'list',   //出发地   apply-申请开票   list-开票列表
        isOverrun: 1
      },
      //有待支付订单&&未跨阶梯弹层
      bePaidConfig: {
        width: '800px',
        visible: false,
        ladderStatus: 1, //1-未跨阶梯  2-跨阶梯
        pendingOrders: 1, //1-有待支付订单  2-无待支付订单
        source: 3,    //来源  1-在途  2-无在途  3-单笔结算  4-批量结算
      },
      balanceCheckList: [],  //跨阶梯传递的选中数据
      balanceCheckNewList: [],  //跨阶梯传递的选中数据
      overLimitCheckList: [],  //批量结算超额弹层传递的选中数据
      toBePaidList: [],  //结算待支付的数据
      studerTypeObj:{
        entIdGroup:{},//
        dataList:[],
      },
    }
  },
  methods: {
    //-------------------------结算与批量结算  start ↓------------------
    //结算
    singleBalance(record){
      this.digitalInfo.pageSource = 3;
      this.digitalInfo.moneyTotal = record.amount;

      this.entId = null;
      this.entId = record.entId;


      //先清空数组 再赋值选中状态追加数据
      let list = [];
      let checkList = cloneObject(record);
      checkList.checkShow = true;
      list = [checkList];
      this.balanceCheckList = list;
      this.overLimitCheckList = list;
      this.balanceCheckNewList = list;

      //调取比对有无进行中的票并且数电额度是否一致
      this.ongoingTicketsDigitalCreditLimit();
    },
    //批量结算
    batchSettlement(){
      if(this.isType.isNullOrUnDef(this.selectedRows) || this.isType.isEmpty(this.selectedRows)){
        this.$message.warning('请选择需要结算的数据');
        return false
      }

      this.digitalInfo.pageSource = 4;
      //选中数据 this.selectedRows
      this.entId = null;
      this.entId = this.selectedRows[0].entId;  //发票企业id
      let total = this.selectedRows.reduce((sum, item) => { return sum + item.amount }, 0);
      this.digitalInfo.moneyTotal = total;    //发票金额总和

      //先清空数组 再赋值数据
      // this.balanceCheckList = [];
      let checkList = cloneObject(this.selectedRows);
      checkList.forEach(obj => {
        obj.checkShow = true;
      });
      this.balanceCheckList = checkList;
      this.overLimitCheckList = checkList;
      this.balanceCheckNewList = checkList;

      //调取比对有无进行中的票并且数电额度是否一致
      this.ongoingTicketsDigitalCreditLimit();
    },
    //-------------------------结算与批量结算（弹层成功和失败回调）  start ↓------------------
    //···情况一： 无待支付订单 && 未跨阶梯   预估费用弹层 ==> 成功回调
    balanceMethod(configure){
      let config = configure;
      config.ladder = false;  //标注为未跨阶梯
      //开票结算生成订单id
      this.getPaymentId(config);
    },
    //···情况二： 有待支付订单 && (未跨阶梯 || 跨阶梯)  有待支付订单选择弹层 ==> 成功回调
    toBePaidMethod(config){
      //关闭有待支付订单弹层
      this.$nextTick(()=> {
        this.bePaidConfig.visible =  false;
      })

      //先清空数组 再赋值数据
      // this.balanceCheckList = [];
      this.balanceCheckList = config.balanceCheckList;
      //ladderStatus  1-未跨阶梯  2-跨阶梯
      if(config.ladderStatus == 1){
        //打开预估费用弹层
        this.$nextTick(()=> {
          this.balanceConfig = {
            width: '800px',
            visible: true,    //modal展示状态
            ladderStatus: config.ladderStatus, //1-未跨阶梯  2-跨阶梯
            pendingOrders: config.pendingOrders, //1-有待支付订单  2-无待支付订单
            source: 4, //3-单笔结算  4-批量结算
          };
        })
      }else {
        //打开确认费用弹层
        this.$nextTick(()=> {
          this.batchConfig = {
            width: '800px',
            ladderStatus: config.ladderStatus, //1-未跨阶梯  2-跨阶梯
            pendingOrders: config.pendingOrders, //1-有待支付订单  2-无待支付订单
            visible: true,    //modal展示状态
            mark: config.mark,
            source: 4, //3-单笔结算  4-批量结算
          };
        })
      }
    },
    //有待支付订单 && 未跨阶梯  确认费用弹层 ==> 返回上一步回调
    backBePaidCancle(data){
      //关闭预估费用弹层
      this.$nextTick(()=> {
        this.balanceConfig.visible =  false;
      })

      //先清空数组 再赋值数据
      // this.balanceCheckList = [];
      this.balanceCheckList = this.balanceCheckNewList;
      //打开有待支付订单弹层
      this.$nextTick(()=> {
        this.bePaidConfig = {
          width: '800px',
          visible: true,    //modal展示状态
          pendingOrders: 1, //1-有待支付订单  2-无待支付订单
          ladderStatus: data.ladderStatus, //1-未跨阶梯  2-跨阶梯
          source: this.digitalInfo.pageSource, //3-单笔结算  4-批量结算
        };
      })
    },
    //···情况三： 无待支付订单 && 超限额  单笔结算提示弹层 ==> 成功回调
    //超限额等超额判断弹层---primary按钮事件
    fatherPromptMethod(data){
      this.$nextTick(()=>{
        this.promptConfig.visible = false;
      })
    },
    //超限额等超额判断弹层---拓展按钮事件
    fatherPreserveMethod(data){
      // console.log('接收到了子组件带来的参数',data)
      this.$nextTick(()=> {
        this.promptConfig.visible = false;
      })

      //
      if(data.source == 3 && data.category == 'czs'){
        //超征收限额
        this.$nextTick(()=>{
          //展示变更征收方式弹层
          this.collectionConfig = {
            width: '800px',
            visible: true,
            source: 3,   //来源   3-单笔结算  4-批量结算
            base: 'list',   //出发地   apply-申请开票   list-开票列表
            isOverrun: 1
          }
        })
      }else {
        this.$nextTick(()=> {
          this.overLimitConfig = {
            width: '750px',
            visible: true,
            category: data.category,
            pendingOrders: data.pendingOrders, //1-有待支付订单  2-无待支付订单
            ladderStatus: data.ladderStatus, //1-未跨阶梯  2-跨阶梯
            source: data.source, //3-单笔结算  4-批量结算
            settlableLimit: data.settlableLimit, //可结算额度
          }
        })
      }

    },
    //···情况四： (有待支付订单 || 无待支付订单) && 超限额  展示超限额等超额判断弹层 ==> 成功回调
    //成功回调 超限额等超额判断弹层（批量结算）
    overLimitMethod(config){
      this.$nextTick(()=>{
        this.overLimitConfig.visible = false;
      })

      //先清空数组 再赋值数据
      // this.balanceCheckList = [];
      this.balanceCheckList = config.balanceCheckList;

      //下一步调取费用弹层
      this.$nextTick(()=> {
        this.batchConfig = {
          width: '850px',
          visible: true,    //modal展示状态
          ladderStatus: config.ladderStatus, //1-未跨阶梯  2-跨阶梯
          pendingOrders: config.pendingOrders, //1-有待支付订单  2-无待支付订单
          category: config.category,  //类别
          source: 4, //3-单笔结算  4-批量结算
        };
      })
    },
    //点击此处---成功回调 超限额等超额判断弹层（批量结算）
    overLimitBackCancle(config){
      this.$nextTick(()=> {
        this.overLimitConfig.visible = false;
      })
      if (config.category == 'sd'){
        //数电
        this.$nextTick(()=>{
          this.promptConfig = {
            width: '650px',
            visible: true,    //modal展示状态
            content: '联系客服，提高数电月核定额度',
            okText: '关闭',    //确定文字
            okType: 'primary',    //确定类型
            cancleText: '',    //取消文字
            cancleType: '',    //取消类型
            expandText: '返回上一步',    //拓展文字
            big: true,    //按钮
            source: 4,    //来源  3-单笔结算  4-批量结算
            category: config.category,    //类别
            pendingOrders: config.pendingOrders, //1-有待支付订单  2-无待支付订单
            settlableLimit: config.settlableLimit, //可结算额度
          };
        })
      }else if (config.category == 'czs'){
        //超征收限额
        this.$nextTick(()=>{
          //展示变更征收方式弹层
          this.collectionConfig = {
            width: '800px',
            visible: true,
            source: 4,   //来源   3-单笔结算  4-批量结算
            base: 'list',   //出发地   apply-申请开票   list-开票列表
            isOverrun: 1,
            pendingOrders: config.pendingOrders, //1-有待支付订单  2-无待支付订单
            defaultCheckList: config.defaultCheckList,  //本次申请
            toPayCheckList: config.toPayCheckList,  //待支付订单
            settlableLimit: config.settlableLimit, //可结算额度
          }
        })
      }
    },
    //···情况五： (有待支付订单 || 无待支付订单) && （跨阶梯 || 超限额）  确认费用弹层 ==> 成功回调
    //跨阶梯成功回调（批量结算）
    batchMethod(configure){
      let config = configure;
      config.ladder = true;  //标注为跨阶梯
      //TODO 判断无已完成的发票 &&  首次开票金额超过您核定额度的1/3
      // let firstStatus = true;
      // if (firstStatus){
      //   this.$nextTick(()=>{
      //     this.excessConfig.visible = true;
      //   })
      // }else {
      //   //开票结算生成订单id
      //   this.getPaymentId(config);
      // }
      //开票结算生成订单id
      this.getPaymentId(config);
    },
    //返回上一步回调 超限额等超额判断弹层（批量结算）
    backLimitCancle(config){
      // console.log('我调到批量结算超额弹层的子组件了',config)
      this.$nextTick(()=> {
        this.batchConfig.visible = false;
      })

      //pendingOrders: 1, //1-有待支付订单  2-无待支付订单
      if (config.mark == 1){
        let list = cloneObject(this.balanceCheckList);
        this.balanceCheckList = list.filter(item => item.payCheckStatus == true);
      }

      //ladderStatus  1-未跨阶梯  2-跨阶梯
      if (config.ladderStatus == 2){
        this.$nextTick(()=> {
          this.bePaidConfig.visible = true;
        })
      }else{
        this.$nextTick(()=> {
          this.overLimitConfig.visible = true;
        })
      }
    },
    //···情况六：  变更征收方式弹层 ==> 成功回调
    //征收方式变更返回回调 返回上一步（批量结算）
    fatherBackMethod(config){
      // console.log('我调到征收方式的子组件了！');
      //关闭变更征收方式弹层
      this.$nextTick(()=> {
        this.collectionConfig.visible = false;
      })

      if(config.source == 3){
        this.$nextTick(()=> {
          this.promptConfig.visible = true;
        })
      }else{
        this.$nextTick(()=> {
          this.overLimitConfig.visible = true;
        })
      }
    },
    //-------------------------公共方法  start ↓------------------
    findIndicesNotContainingValue(arr, value) {
      const indices = [];
      for (let i = 0; i < arr.length; i++) {
        if (arr[i] !== value) {
          indices.push(i); // 收集不等于给定值的元素的索引
        }
      }
      return indices; // 返回索引数组
    },
  }
}

export  default  balance
